# encoding=utf-8
from requests_html import HTMLSession
from lxml import etree
from entity.entity import category
from storages.cache import cache
from constant.const import const
from utils.string import getUuid
from log.log import log

session = HTMLSession()
c = cache()

base_url = 'https://www.bbiquge.net/'

header = {
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cookie": "HMACCOUNT_BFESS=07B85514E05D22AE; H_WISE_SIDS_BFESS=110085_127969_179349_180636_185638_188749_189755_191527_194085_194519_196428_196527_197471_197711_197948_199572_203518_204916_207236_207697_208721_209568_210321_210732_210757_211953_212295_212726_212739_212797_212867_212912_213039_213059_213094_213289_213357_213484_214094_214205_214379_214596_214652_214791_214884_215070_215127_215280_215333_215554_215730_215859_215892_216043_216253_216297_216335_216342_216353_216368_216446_216596_216631_216634_216645_216843_217183_217320_217410_217439_217453_217514_217760_217868; BAIDUID_BFESS=FD30747E577715F2704ED40BB1AE3016:FG=1; BDUSS_BFESS=VJSY3dVTEpVN34ycjJFUjRjanA0Qy00UWR5allmVWFkTVE0enI3a21iUDduU05rRVFBQUFBJCQAAAAAAAAAAAEAAABJszI0xfS71L3wxaMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPsQ~GP7EPxjO; ZFY=1S3:AvULhrXM1QTkZp:AmNtEmf:AUoPgmjyb8voM0q2j:BE:C",
    "Host": "hm.baidu.com",
    "Referer": "https://www.bbiquge.net/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
}


# 抽取的基础爬取功能
def process(url, params):
    try:
        response = session.get(url=url, params=params, timeout=(5, 10), proxies=const.proxies)
        # response = session.get(url=url, params=params)
        response.encoding = response.apparent_encoding
        return etree.HTML(response.text)
    except Exception as e:
        log("../../log/base.log", e)


# 导航栏爬取
def get_category(url, params):
    html = process(url, params)
    text = html.xpath("//body/div[@class='nav-bar']/div[@class='nav_cont']/ul/li/a/text()")
    href = html.xpath("//body/div[@class='nav-bar']/div[@class='nav_cont']/ul/li/a/@href")
    category_arr = []
    for i in range(len(href)):
        fenlei = ""
        urls = ""
        if i != 0 and i != 7 and i != 8:
            fenlei = const.fenlei + str(i)
            urls = href[0] + href[i]
        else:
            if i == 0:
                fenlei = "home"
                urls = href[0]
            elif i == 7:
                urls = href[0] + href[i]
                fenlei = "quanben"
            else:
                urls = href[0] + href[i]
                fenlei = "monthvisit"
        cate = category()
        cate.id = getUuid()
        cate.name = text[i]
        cate.category = fenlei
        cate.uri = urls
        category_arr.append(cate)
        if c.get(const.category, fenlei, const.map) is None:
            c.map(const.category, fenlei, urls)
        if c.get(const.category_entity, fenlei, const.map) is None:
            c.map(const.category_entity, fenlei, str(cate))
    return category_arr

# get_category(base_url, header)
